package com.app.utils.base.j2se;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.PrintWriter;
import java.io.StringWriter;

@Component
public class Logger {


	private static org.slf4j.Logger logger = LoggerFactory.getLogger(Logger.class);

	public static void info(Object message) {
		logger.info(Convert.objToStr(message));
	}

	public static void info(String message,Object... args) {
		logger.info(String.format(message,args));
	}
	
	public static void debug(Object message) {
		logger.debug(Convert.objToStr(message));
	}

	public static void debug(String message,Object... args) {
		logger.debug(String.format(message,args));
	}

	@Value("${spring.profiles.active}")
	private String active;
	private static String env;
	@PostConstruct
	public void init(){
		env = this.active;
	}

	/** 获取当前环境 */
	public static String getEnv(){
		return env;
	}

	public static boolean isOnline(){
		return env.equals("prod");
	}
	public static void error(Object object) {
		if(object instanceof Throwable){
			logger.error(exceptionToString((Throwable)object));
		}else {
			logger.error((String) object);
		}
	}

	public static void error(String message,Throwable e) {
		logger.error(String.format(message.contains("%s")?message:(message+"%s"),exceptionToString(e)));
	}

	public static String exceptionToString(Throwable e){
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw, true);
		e.printStackTrace(pw);
		pw.flush();
		sw.flush();
		return sw.toString();
	}
}
